package com.zhentao.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhentao.domain.SLearningProgress;
import com.zhentao.domain.SLevel;
import com.zhentao.domain.SSubject;
import com.zhentao.domain.dto.SubjectDto;
import com.zhentao.domain.dto.subjectExport;
import com.zhentao.mapper.SLearningProgressMapper;
import com.zhentao.mapper.SLevelMapper;
import com.zhentao.result.Result;
import com.zhentao.service.SSubjectService;
import com.zhentao.mapper.SSubjectMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
* @author 86150
* @description 针对表【s_subject】的数据库操作Service实现
* @createDate 2024-07-23 11:11:35
*/
@Service
public class SSubjectServiceImpl extends ServiceImpl<SSubjectMapper, SSubject>
    implements SSubjectService{


    @Resource
    private SSubjectMapper sSubjectMapper;

    @Resource
    private SLevelMapper sLevelMapper;

    @Resource
    private SLearningProgressMapper sLearningProgressMapper;


    //查询课程列表
    @Override
    public Result getSubjectList(SubjectDto subjectDto) {
        Page<SSubject> page = new Page<>(subjectDto.getPageNum(), subjectDto.getPageSize());
        Page<SSubject> list= sSubjectMapper.getSubjectList(page,subjectDto);
        return Result.OK(list);
    }

    //获取阶段名称（专高/专业/实训）
    @Override
    public Result getLevelList() {
        List<SLevel> sLevels = this.sLevelMapper.selectList(null);
        return Result.OK(sLevels);
    }

    //获取具体阶段名称（专业一/专业二....）
    @Override
    public Result getSLearningProgressList() {
        List<SLearningProgress> sLearningProgresses = this.sLearningProgressMapper.selectList(null);
        return Result.OK(sLearningProgresses);
    }



    //添加课程
    @Override
    public Result addSubject(SSubject sSubject) {
        if (sSubject.getLevelId() == null){
            return Result.ERROR("请选择阶段");
        }else if (sSubject.getLpId() == null){
            return Result.ERROR("请选择具体阶段");
        }else if (sSubject.getSubjectName() == null){
            return Result.ERROR("请输入课程名称");
        }else {
            sSubjectMapper.insert(sSubject);
            return Result.OK("添加成功");
        }
    }



    //根据课程id查找课程
    @Override
    public Result getSubjectById(Integer id) {
        SSubject sSubject = this.sSubjectMapper.selectById(id);
        return Result.OK(sSubject);
    }

    //修改课程
    @Override
    public Result updateSubject(SSubject sSubject) {
        int i = this.sSubjectMapper.updateById(sSubject);
        return Result.OK(i);
    }

    @Override
    public Result delSubject(Integer id) {
        int i = this.sSubjectMapper.deleteById(id);
        return Result.OK(i);
    }



    //获取到导出的数据
    @Override
    public List<subjectExport> getSubjectExport() {
        List<SSubject> list = this.sSubjectMapper.selectList(null);
        List<subjectExport> list1=new ArrayList<>();
        for (SSubject sSubject : list){
            subjectExport subjectExport = new subjectExport();
            subjectExport.setId(sSubject.getId());
            subjectExport.setSubjectName(sSubject.getSubjectName());
            SLevel sLevel = this.sLevelMapper.selectById(sSubject.getLevelId());
            subjectExport.setLevelName(sLevel.getLevelName());
            SLearningProgress sLearningProgress = this.sLearningProgressMapper.selectById(sSubject.getLpId());
            subjectExport.setName(sLearningProgress.getName());
            list1.add(subjectExport);
        }
        return list1;
    }

}




